<!--
 * @Author: 奶龙也是龙 15104937352@163.com
 * @Date: 2025-09-11 08:36:57
 * @LastEditors: 奶龙也是龙 15104937352@163.com
 * @LastEditTime: 2025-09-17 15:19:12
 * @FilePath: \大三前端\city\src\pages\components\Left2.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <Title :title="title"></Title>
        <!-- 1.提供图标容器 -->
        <div ref="bottom2" style="height: 150px;margin-top: 30px;"></div>
    </div>
</template>
<script>
import { Column } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';
export default {
    data() {
        return {
            title: '实时噪声大小',
            params: {
                page: 1,
                pageSize: 100
            },
            tempData: [],
            column:null
        }
    },
    computed: {
        ...mapState(['ed'])
    },
    //侦听器 侦听数据
    watch: {
        ed: {
            //根据设备信息重新查询数据重新渲染图表
            handler(n, o) {
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getTempData();
                //根据获取的数据重新渲染图表
                this.column.changeData(this.tempData);
            },
            //深度监听
            deep: true
        }
    },
    methods: {
        loadLine() {
            this.column = new Column(this.$refs.bottom2, {
                data: this.tempData,
                xField: 'insert_time',
                yField: 'noise',
                xAxis: {
                    label: {
                        autoRotate: false,
                    },
                },
                slider: {
                    start: 0.1,
                    end: 0.2,
                },
            });

            this.column.render();
        },
        async getTempData() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            console.log(res, "获取响应");
            let temp = [];
            // 将后端获取的数据设置给tempData
            res.data.list.forEach((item, index, arr) => {
                //由于刻度混乱
                arr[0].noise = 45;
                // item表示数组每一项

                item.insert_time = moment(item.insert_time).format("HH:mm");
                //由于数据查询太密集 需要将数据处理 每隔10条获取数据
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }
            });
            this.tempData = temp;
        }

    },
    async mounted() {
        await this.getTempData();
        this.loadLine();
    }
}
</script>
<style scoped></style>